from turtle import *
from random import *
from time import sleep
# 数据 -----------------------------------------------------------------
scrn_w = 1024
scrn_h = 768

x_limit = scrn_w // 2 - 20
y_limit = scrn_h // 2 - 20

cat_speed = 1
rat_num = 1
rats = []
register_shape('gif图.gif')

used_time = 0


class MyPen(Turtle):
    def __init__(self, x, y, color_):
        super().__init__()
        self.pu()
        self.setpos(x, y)
        self.color(color_)
        self.ht()


pen1 = MyPen(300, 320, 'blue')
# 界面---------------------------------
# 窗口--------------------------------
setup(scrn_w, scrn_h)
bgpic('gif图.gif')
listen()
tracer(3)

pen1.write('计时: ', font=('宋体', 20, 'bold'))

# 猫-------------------------------
cat = Turtle()
cat.resizemode('user')
cat.shapesize(4, 3, 5)
cat.up()

# 鼠----------------------------------
for i in range(rat_num):
    rat = Turtle()
    rat.up()
    rat.shape('gif图.gif')
    rat.setpos(randint(-x_limit, x_limit), randint(-y_limit, y_limit))
    rat.seth(randint(0, 360))
    rats.append(rat)

# 逻辑 ------------------------------------
pen1 = MyPen(400, 320, 'blue')


def update_time():
    global used_time
    used_time += 1
    pen1.clear()
    pen1.write(str(used_time), font=('宋体', 20, 'bold'))
    ontimer(update_time, 1000)


update_time()
print(used_time)


def turn_left():
    cat.left(15)


def turn_right():
    cat.right(15)


def speedup():
    global cat_speed
    if cat_speed < 10:
        cat_speed += 1


def slowdown():
    global cat_speed
    if cat_speed > 1:
        cat_speed -= 1


onkey(turn_left, 'Left')
onkey(turn_right, 'Right')
onkey(speedup, 'Up')
onkey(slowdown, 'Down')

while 1:
    # 猫的运动及控制-----------------------------
    cat.fd(cat_speed)
    x = cat.xcor()
    y = cat.ycor()
    if x < -x_limit or x > x_limit or y < -y_limit or y > y_limit:
        cat.right(180)

    for rat in rats:
        rat.fd(1)
        x = rat.xcor()
        y = rat.ycor()
        if x < -x_limit or x > x_limit or y < -y_limit or y > y_limit:
            rat.right(180)

        if rat.distance(cat) < 10:
            rat.ht()
            rats.remove(rat)

    if len(rats) == 0:
        break
_MAPPING = (u'零', u'一', u'二', u'三', u'四', u'五', u'六', u'七', u'八', u'九', u'十', u'十一', u'十二', u'十三', u'十四', u'十五', u'十六', u'十七',u'十八', u'十九')
_P0 = (u'', u'十', u'百', u'千',)
_S4 = 10 ** 4
def _to_chinese4(num):
    assert (0 <= num and num < _S4)
    if num < 20:
        return _MAPPING[num]
    else:
        lst = []
        while num >= 10:
            lst.append(num % 10)
            num = num / 10
        lst.append(num)
        c = len(lst)  # 位数
        result = u''

        for idx, val in enumerate(lst):
            val = int(val)
            if val != 0:
                result += _P0[idx] + _MAPPING[val]
                if idx < c - 1 and lst[idx + 1] == 0:
                    result += u'零'
        return result[::-1]
pen2 = MyPen(0,0,'red')
pen2.write('游戏结束，用时%s秒'%_to_chinese4(used_time), align='center',font=('微软雅黑', 25, 'bold'))
sleep(100)